<template>
  <div id="user-manage-page">
    <!--    搜索-->
    <a-form layout="inline">
      <a-form-item label="账号">
        <a-input
          v-model:value="params.userAccount"
          @pressEnter="doSearch"
          placeholder="根据账号查询"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="用户名">
        <a-input
          v-model:value="params.userName"
          @pressEnter="doSearch"
          placeholder="根据用户名查询"
          allow-clear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="doSearch">查询</a-button>
      </a-form-item>
    </a-form>

    <a-divider />

    <!--    表格-->
    <a-table
      :columns="columns"
      :data-source="userList"
      :pagination="pagination"
      @change="doTableChange"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'id'">
          <a>
            {{ record.id }}
          </a>
        </template>
        <template v-else-if="column.dataIndex === 'userAvatar'">
          <a-image :height="80" :width="80" :src="record.userAvatar" />
        </template>
        <template v-else-if="column.dataIndex === 'userProfile'"></template>
        <template v-else-if="column.dataIndex === 'userRole'">
          <a-tag color="green" v-if="record.userRole === 'admin'">管理员</a-tag>
          <a-tag color="blue" v-else>普通用户</a-tag>
        </template>

        <template v-else-if="column.dataIndex === 'createTime'">
          {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-else-if="column.key === 'action'">
          <span>
            <a-popconfirm @confirm="doDeleteUser(record.id)" title="你确定要删除吗？">
              <a-button danger>删除</a-button>
            </a-popconfirm>
          </span>
        </template>
      </template>
    </a-table>
  </div>
</template>
<script lang="ts" setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { listUserByPageUsingGet, deleteUserUsingPost } from '@/api/userController.ts'
import dayjs from 'dayjs'

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
  },
  {
    title: '账号',
    dataIndex: 'userAccount',
  },
  {
    title: '用户名',
    dataIndex: 'userName',
  },
  {
    title: '头像',
    dataIndex: 'userAvatar',
  },
  {
    title: '简介',
    dataIndex: 'userProfile',
  },
  {
    title: '用户角色',
    dataIndex: 'userRole',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '操作',
    key: 'action',
  },
]

// 请求参数
const params = reactive<API.listUserByPageUsingGETParams>({
  current: 1,
  pageSize: 10,
  sortField: 'create_time',
  sortOrder: 'ascend',
})

//  total
const total = ref(0)
// 获取数据
const userList = ref<API.UserVO[]>([])

const getUserList = async () => {
  const res = await listUserByPageUsingGet({ ...params })
  if (res.data.code !== 0) {
    return
  }
  userList.value = res.data.data.records
  total.value = res.data.data.total
  console.log(res.data)
}

// 分页数据
const pagination = computed(() => {
  return {
    current: params.current,
    showQuickJumper: true,
    pageSize: params.pageSize,
    total: total.value,
    showSizeChanger: true,
    pageSizeOptions: ['5', '10', '20'],
    showTotal: (total) => `共 ${total} 条`,
  }
})

// 分页改变
const doTableChange = (page) => {
  params.current = page.current
  params.pageSize = page.pageSize
  getUserList()
}

// 查询
const doSearch = () => {
  params.current = 1
  getUserList()
}

// 删除用户
const doDeleteUser = async (id: string) => {
  const res = await deleteUserUsingPost({ id: id })
  if (res.data.code !== 0) {
    return
  }
  await getUserList()
}

// 页面渲染完成时，获取数据
onMounted(() => {
  getUserList()
})
</script>
